<template>
  <div class="list" ref="wrapper">
    <div>
      <div class="area">
        <div class="title border-topbottom">当前城市</div>
        <div class="button-list">
          <div class="buttton-wrapper">
            <div class="button">{{this.$store.state.city}}</div>
            <!-- 真军火库，优雅的修改变量，集中式数据流的  管理方案 -->
          </div>
        </div>
      </div>
      <div class="area">
        <div class="title border-topbottom">热门城市</div>
        <div class="button-list">
          <div class="buttton-wrapper" v-for="item of hotCities" :key="item.id" @click="handleCityClick(item.name)">
            <!-- 改变首页显示城市名，vuex 事件订阅，evnetbus 事件总线 -->
            <div class="button">{{item.name}}</div>
          </div>
        </div>
      </div>
      <div class="area" v-for="(i,key) in cities" :key="key" :ref="key">
        <div class="title border-topbottom">{{key}}</div>
        <div class="item-list">
          <div class="item border-bottom" v-for="t in i" :key="t.id" @click="handleCityClick(t.name)">
            {{t.name}}</div>

        </div>
      </div>
    </div>
  </div>
</template>

<script>
import Bscroll from 'better-scroll'
import { mapState, mapMutations } from 'vuex'
export default {
  name: 'citylist',
  props: {
    hotCities: Array,
    cities: Object,
    letter: String
  },
  data() {
    return {
      scroll: ''
    }
  },
  computed: {
    ...mapState({
      currentCity: 'city'
    })
  },
  methods: {
    ...mapMutations(['changeCity']),
    //把store中的mutations 中的changeCity方法映射到组件 这个方法 this.changeCity就可以用了
    handleCityClick(city) {
      // console.log(1)
      // this.$store.commit('changeCity', city)
      this.changeCity(city)
      this.$router.push('/')
    }
  },
  mounted() {
    this.scroll = new Bscroll(this.$refs.wrapper)
  },
  watch: {
    //侦听器，letter变时做点事
    letter() {
      if (this.letter) {
        const element = this.$refs[this.letter][0]
        // $refs 标签dom 组件组件实例对象
        this.scroll.scrollToElement(element)
        // console.log(element)
        // better-scroll 提供跳转到指定区域
        // 监听letter，letter一变，用滚动到方法，滚到那  alphabet - city -传不同letter-list -list 滚动
      }
      // console.log(this.letter)
    }
  }
}
</script>
<!-- stylus &符合之上的所有 上右下左-->
<style lang="stylus" scoped>
.border-topbottom {
  &:before {
    border-color: #ccc;
  }

  &:after {
    border-color: #ccc;
  }
}

.border-bottom {
  &:before {
    border-color: #ccc;
  }
}

.list {
  position: absolute;
  overflow: hidden;
  top: 1.58rem;
  left: 0;
  bottom: 0;
  right: 0;
}

.title {
  line-height: 0.44rem;
  background: #eee;
  padding-left: 0.2rem;
  color: #666;
  font-size: 0.26rem;
}

.button-list {
  padding: 0.1rem 0.6rem 0.1rem 0.1rem;
  overflow: hidden;
}

.buttton-wrapper {
  width: 33.33%;
  float: left;
}

.button {
  text-align: center;
  margin: 0.1rem;
  padding: 0.1rem 0;
  border: 0.02rem solid #ccc;
  border-radius: 0.06rem;
}

.item-list {
}

.item {
  line-height: 0.48rem;
  color: #666;
  padding-left: 0.2rem;
}
</style>